package asapIs134CK.womi.client.games.logoGame.froggitTheFrog;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import asapIs134CK.womi.client.games.GamePickerWidget;
import asapIs134CK.womi.client.games.GameWidgetsFactory;
import asapIs134CK.womi.client.games.PickupEvent;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FroggitPickerWidget extends GamePickerWidget {

	private List<FroggitWidgetsFactory> _factories;
	
	private Panel _container;
	private FlexTable _allMap;
	public FroggitPickerWidget(){
		super();
		_factories = new LinkedList<FroggitWidgetsFactory>();
		_container = new VerticalPanel();
		_allMap = new FlexTable();
		loadList();
		initGUI();
		initWidget(_container);
	}
	private void loadList() {
		//TODO add every widgetFactory by geting all differents world from the server
		_factories.add(new FroggitWidgetsFactory());
		//Temp fix : just add the default map
	}
	private void initGUI() {
		_container.setStyleName("froggitTF_thumbnail");
		_container.add(new Label("Please, choose one Map for Froggit"));
		
		
		Iterator<FroggitWidgetsFactory> ite = _factories.iterator();
		int column, maxcolumn;
		column = 0; maxcolumn = 6;
		int rowCount = 0;
		while(ite.hasNext()){
			System.out.println("we get one factory here");
			if(column >= maxcolumn){
				column=0;
				_allMap.insertRow(++rowCount);
			}
			final FroggitWidgetsFactory fact = ite.next();
			FroggitThumbnail thumb = fact.createThumbnailWidget();
			thumb.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					notifyPickUp(new PickupEvent() {
						
						@Override
						public GameWidgetsFactory getPickedUp() {
							return fact;
						}
					});
				}
			});
			_allMap.setWidget(rowCount, column, thumb);
		}
		_container.add(_allMap);
		//TODO put some code to fill the table with blank ...
	}
}
